//package com.tenebrous.jestic.grpc;
//
//import com.google.protobuf.Any;
//import com.tenebrous.jestic.auth.AuthTokenGenerator;
//import com.tenebrous.jestic.entity.server.JesticServer;
//import com.tenebrous.jestic.proto.common.GrpcResponse;
//import com.tenebrous.jestic.proto.common.RetCode;
//import com.tenebrous.jestic.service.server.AuthServerService;
//import com.zy.tenebrous.jestic.utils.AESCodec;
//import io.grpc.stub.StreamObserver;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.server.service.GrpcService;
//import org.springframework.beans.factory.annotation.Autowired;
//
///**
// * @Description:
// * @Project: jestic-server
// * @Package: com.tenebrous.jestic.v3.member
// * @Author: zhangya
// * @CreateTime: 2023-06-01  18:27
// * @Version: 1.0.0
// */
//@Slf4j
//@GrpcService
//public class SechubCryptographyImpl extends CryptographyServiceGrpc.CryptographyServiceImplBase {
//
//    @Autowired
//    AuthServerService authServerService;
//
//    @Autowired
//    AuthTokenGenerator authTokenGenerator;
//
//    @Override
//    public void encryption(CryptographyGrpc.EncryptionRequest request, StreamObserver<GrpcResponse> responseObserver) {
//        log.info("\nSechubCryptographyImpl.encryption.request:\n" +
//                "***********************************************\n{}\n" +
//                "***********************************************", request);
//        JesticServer jesticServer = authServerService.selectServer(request.getServerId(), request.getServerName());
//        GrpcResponse grpcResponse = null;
//        try {
//            String encode = AESCodec.encrypt(request.getPwd(), jesticServer.getServerKey());
//            CryptographyGrpc.EncryptionResponse response = CryptographyGrpc.EncryptionResponse.newBuilder().setResult(encode).build();
//            grpcResponse = GrpcResponse.newBuilder().setRetCode(RetCode.SUCCESS).setRetMsg("success").setData(Any.pack(response)).build();
//        } catch (Exception e) {
//            CryptographyGrpc.EncryptionResponse response = CryptographyGrpc.EncryptionResponse.newBuilder().build();
//            grpcResponse = GrpcResponse.newBuilder().setRetCode(RetCode.BIZ_ERROR).setRetMsg("JESTIC AES decrypt fail:" + e.getMessage()).setData(Any.pack(response)).build();
//            log.error("JESTIC AES decrypt fail:[{}]", e.getMessage());
//        }
//        responseObserver.onNext(grpcResponse);
//        responseObserver.onCompleted();
//    }
//
//    @Override
//    public void decryption(CryptographyGrpc.DecryptionRequest request, StreamObserver<GrpcResponse> responseObserver) {
//        log.info("\nSechubCryptographyImpl.decryption.request:\n" +
//                "***********************************************\n{}\n" +
//                "***********************************************", request);
//        JesticServer jesticServer = authServerService.selectServer(request.getServerId(), request.getServerName());
//        GrpcResponse grpcResponse = null;
//        try {
//            String encode = AESCodec.decrypt(request.getPwd(), jesticServer.getServerKey());
//            CryptographyGrpc.DecryptionResponse response = CryptographyGrpc.DecryptionResponse.newBuilder().setResult(encode).build();
//            grpcResponse = GrpcResponse.newBuilder().setRetCode(RetCode.SUCCESS).setRetMsg("success").setData(Any.pack(response)).build();
//        } catch (Exception e) {
//            CryptographyGrpc.DecryptionResponse response = CryptographyGrpc.DecryptionResponse.newBuilder().build();
//            grpcResponse = GrpcResponse.newBuilder().setRetCode(RetCode.BIZ_ERROR).setRetMsg("JESTIC AES decrypt fail:" + e.getMessage()).setData(Any.pack(response)).build();
//            log.error("JESTIC AES decrypt fail:[{}]", e.getMessage());
//        }
//        responseObserver.onNext(grpcResponse);
//        responseObserver.onCompleted();
//    }
//}
